// pages/my/service-comment/service-comment.js
const controllers = require('../../controllers/ControllerMy').controller;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype: '',
    shopPoint: 0,
    masterPoint: 0,
    issubmit: false,
    imgurl: '',
    commentinput: '',
    files: [],
    imgstring: '',
    isstar: false,
    imglist: [],
    type: 0,
    orderNo: 0,
    issubmit02: false,
    oneButton: [{
      text: '确定'
    }],
  },
  //删除图片
  delete(e) {
    console.log(e);
    // this.setData({
    //   imglist:this.data.imglist.splice(e.detail.index,1),
    //   files:this.data.files.splice(e.detail.index,1)
    // })
    var imgar = this.data.imglist;
    var filesarr = this.data.files;
    imgar.splice(e.detail.index, 1)
    filesarr.splice(e.detail.index, 1)
    this.setData({
      imglist: imgar,
      files: filesarr
    })
  },
  //关闭弹框
  tapDialogButton(e) {
    this.setData({
      issubmit02: false
    })
  },
  //评分
  scorefrom(e) {
    this.setData({
      masterPoint: e.detail
    })
  },
  //输入评论
  inputcomment(e) {
    this.setData({
      commentinput: e.detail.value
    })
    if (e.detail.value.length >= 200) {
      let str = this.data.commentinput
      this.setData({
        issubmit02: true,
        commentinput: str.substr(0, 20)
      })
    } else {
      this.setData({
        issubmit02: false
      })
    }
  },
  //上传图片
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  selectFile(files) {
    console.log('files', files)
    // 返回false可以阻止某次文件上传
  },
  uplaodFile(files) {
    console.log('upload files', files)
      // 文件上传的函数，返回一个promise
      return new Promise((resolve, reject) => {
        for (let i = 0; i <files.tempFilePaths.length; i++) {

        wx.uploadFile({
          url: app.globalData.imguploadurl + '/api/img/uploadFile',
          filePath: files.tempFilePaths[i],
          name: 'image',
          formData: {
            sessionId: wx.getStorageSync('usertoken')
          },
          success: (res) => {
            console.log(JSON.parse(res.data).data);

            let arr = this.data.files;
            let arr02 = this.data.imglist
            arr.push({
              url: JSON.parse(res.data).data
            })
            arr02.push(JSON.parse(res.data).data)
            this.setData({
              files: arr,
              imglist: arr02
            })
            resolve(JSON.parse(res.data).data)
          }
        })
      }

        // setTimeout(() => {
        //   reject('some error')
        // }, 4000)

      })

  },
  uploadError(e) {
    console.log('upload error', e.detail)
  },
  uploadSuccess(e) {
    console.log('upload success', e.detail)
  },
  //提交评论
  toSubmit() {
    if (this.data.masterPoint <= 0) {
      wx.showToast({
        title: '请评分',
        icon: 'none'
      })

      return false
    }

    if (this.data.commentinput == '') {
      wx.showToast({
        title: '请输入评价内容',
        icon: 'none'
      })

      return false
    }

    //提交
    if(this.data.type==0){
      controllers.toCommentProduct({
        type: this.data.type,
        orderNo: this.data.orderNo,
        images: this.data.imglist.toString(),
        content: this.data.commentinput,
        technicianScore: this.data.masterPoint
      }).then(res => {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 1000,
          success: function () {
            wx.navigateBack({
              complete: (res) => {},
            })
          }
        })
      })
    }else{
      controllers.toCommentProduct({
        type: this.data.type,
        orderNo: this.data.orderNo,
        images: this.data.imglist.toString(),
        content: this.data.commentinput,
        shopScore: this.data.masterPoint
      }).then(res => {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 1000,
          success: function () {
            wx.navigateBack({
              complete: (res) => {},
            })
          }
        })
      })
    }
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  //预览图片
  topre(e) {
    wx.previewImage({
      urls: [e.currentTarget.id],
    })
  },
  onLoad: function (options) {
    this.setData({
      globalStype: app.globalData.globalStype
    })
    console.log(options.from);

    this.setData({
      imgurl: app.globalData.IMG_URL
    })
    this.setData({
      orderNo: options.id
    })


    if (options.from == 's') {
      this.setData({
        isstar: true,
      })
    } else if (options.from == 'p') {
      this.setData({
        type: 1
      })
    } else {
      this.setData({
        type: 2
      })
    }


    if (options.type == 1) {
      this.setData({
        issubmit: true,
      })
    } else {
      //获取评价
      if(this.data.type==0){
        controllers.getOrderComment({
          orderNo: options.id,
          type: this.data.type
        }).then(res => {
          console.log(res);
          this.setData({
            commentinput: res.data.comment,
            masterPoint: res.data.technicianScore,
            imglist: res.data.imgList
          })
        })
      }else{
        controllers.getOrderComment({
          orderNo: options.id,
          type: this.data.type
        }).then(res => {
          console.log(res);
          this.setData({
            commentinput: res.data.comment,
            masterPoint: res.data.shopScore,
            imglist: res.data.imgList
          })
        })
      }
      
    }

    this.setData({
      selectFile: this.selectFile.bind(this),
      uplaodFile: this.uplaodFile.bind(this)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})